# $Id: PKGBUILD 180326 2013-03-20 13:37:40Z allan $
# Maintainer: Allan McRae <allan@archlinux.org>
# Contributor: judd <jvinet@zeroflux.org>

pkgname=ncurses
pkgver=5.9
pkgrel=5
pkgdesc="System V Release 4.0 curses emulation library"
arch=('i686' 'x86_64')
url="http://www.gnu.org/software/ncurses/"
license=('MIT')
depends=('glibc')
source=(ftp://ftp.gnu.org/pub/gnu/${pkgname}/${pkgname}-${pkgver}.tar.gz{,.sig})
md5sums=('8cb9c412e5f2d96bc6f459aa8c6282a1'
         '014ffdbbfec6d41a9a89d6cbe6434638')

build() {
  cd ${srcdir}/
  mkdir ncurses{,w}-build

  cd ${srcdir}/ncursesw-build
  ../${pkgname}-${pkgver}/configure --prefix=/usr --mandir=/usr/share/man \
     --with-shared --with-normal --without-debug --without-ada \
     --enable-widec --enable-pc-files
  # add --enable-ext-colors and --enable-ext-mouse with next soname bump
  make

  # libraries for external binary support 
  cd ${srcdir}/ncurses-build
  [ $CARCH = "x86_64" ] && CONFIGFLAG="--with-chtype=long"
  ../${pkgname}-${pkgver}/configure --prefix=/usr \
    --with-shared --with-normal --without-debug --without-ada $CONFIGFLAG
  make
}

package() {
  cd ${srcdir}/ncursesw-build
  make DESTDIR=${pkgdir} install

  # fool packages looking to link to non-wide-character ncurses libraries
  for lib in ncurses form panel menu; do
    echo "INPUT(-l${lib}w)" >${pkgdir}/usr/lib/lib${lib}.so
    ln -s lib${lib}w.a ${pkgdir}/usr/lib/lib${lib}.a
  done
  ln -s libncurses++w.a ${pkgdir}/usr/lib/libncurses++.a

  for lib in ncurses ncurses++ form panel menu; do
    ln -s ${lib}w.pc ${pkgdir}/usr/lib/pkgconfig/${lib}.pc
  done

  # some packages look for -lcurses during build
  echo "INPUT(-lncursesw)" >${pkgdir}/usr/lib/libcursesw.so
  ln -s libncurses.so ${pkgdir}/usr/lib/libcurses.so
  ln -s libncursesw.a ${pkgdir}/usr/lib/libcursesw.a
  ln -s libncurses.a ${pkgdir}/usr/lib/libcurses.a

  # non-widec compatibility libraries
  cd ${srcdir}/ncurses-build
  for lib in ncurses form panel menu; do
    install -Dm755 lib/lib${lib}.so.${pkgver} ${pkgdir}/usr/lib/lib${lib}.so.${pkgver}
    ln -s lib${lib}.so.${pkgver} ${pkgdir}/usr/lib/lib${lib}.so.5
  done

  # install license, rip it from the readme
  cd ${srcdir}/${pkgname}-${pkgver}
  install -dm755 ${pkgdir}/usr/share/licenses/$pkgname
  grep -B 100 '$Id' README > ${pkgdir}/usr/share/licenses/${pkgname}/license.txt
}
